<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>Hello MUI</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<link rel="stylesheet" href="../css/mui.min.css">
		<style>
			html,
			body {
				background-color: #efeff4;
			}
			
			.title {
				padding: 20px 15px 10px;
				color: #6d6d72;
				font-size: 15px;
				background-color: #fff;
			}
		</style>
	</head>

	<body>
		<div id="pullrefresh" class="mui-content mui-scroll-wrapper">
			<div class="mui-scroll">
				<ul class="mui-table-view mui-table-view-chevron">
				</ul>
			</div>
		</div>
		<script src="../js/mui.js"></script>
		<script>
			mui.init({
				swipeBack: false,
				pullRefresh: {
					container: '#pullrefresh',
					down: {
						style:'circle',
						callback: pulldownRefresh
					}
				}
			});
			mui.plusReady(function(){
			     getData()
				 addDataEventListen()
			});
			function getData(){
								mui.ajax('http://124.70.73.91:29/music/',{
									data:{
									},
									dataType:'json',//服务器返回json格式数据
									type:'get',//HTTP请求类型
									success:function(res){
										// const tags = parseTag(res.data, 'span'); // ['Hello']
										// const musicList = tags.filter(item => item !== "..")
										const musicList = (res+'').split(',').filter(item =>  item.endsWith('.mp3') )
										localStorage.setItem('allMusicName',musicList)
										renderTable(musicList)
									}
								});
				function	parseTag(str, tag){
								  const regex = new RegExp(`<${tag} class="name">(.*?)</${tag}>`, 'gi');
								  let match;
								  const result = [];
								  while (match = regex.exec(str)) {
									result.push(match[1]);
								  }
								  return result;
				}	
				function renderTable(tableList){
					console.log("执行了渲染方法" +tableList)
					var table = document.body.querySelector('.mui-table-view');
					table.innerHTML = ""
					for(let i=0;i<tableList.length;i++){
						let item = tableList[i]
						let li = document.createElement('li');
						li.setAttribute('id',item)
						li.className = 'mui-table-view-cell';
						li.innerHTML = '<a class="mui-navigate-right" >' + (i + 1) + '<span class="music-name">'+item+'</span></a>';
						table.append(li);
					}
				}			
			}
			/**
			 * 下拉刷新具体业务实现
			 */
			function pulldownRefresh() {
					getData()
					mui('#pullrefresh').pullRefresh().endPulldownToRefresh(); //refresh completed
					mui.toast('下拉刷新成功');
			}
			function addDataEventListen(){
				mui(".mui-table-view").on('tap','.mui-table-view-cell',function(e){
				  //获取id
				   var id = this.getAttribute("id");
				  //打开新闻详情
				  localStorage.setItem('musicName',id)
				  mui.openWindow({
				    id:'tab-webview-subpage-contact',
				    url:'tab-webview-subpage-contact.html',
				  });
				}) 
			}
		</script>
	</body>

</html>